package com.yudean.esb.service.impl.mds;

import com.yudean.esb.dao.mds.MdsArCustomersDao;
import com.yudean.esb.domain.mds.MdsArCustomers;
import com.yudean.esb.service.MdsArCustomersService;
import com.yudean.esb.util.StringUtil;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.jws.WebService;
import java.util.List;

@WebService(endpointInterface = "com.yudean.esb.service.MdsArCustomersService", serviceName = "MdsArCustomersServiceService",targetNamespace = "http://service.esb.yudean.com/")
@Service
@Scope("prototype")
public class MdsArCustomersServiceImpl implements MdsArCustomersService {
    private static final org.slf4j.Logger log = LoggerFactory.getLogger(MdsArCustomersServiceImpl.class);

    @Resource
    private MdsArCustomersDao mdsArCustomersDao;
    /**
     * 按客户名称模糊查询获取ERP客户基本资料指标数据的信息
     * @param customerName  客户名称
     * @return              XML客户基本资料数据
     */
    @Override
    public String getErpArCustomersByName(String customerName) {
        log.info("getErpArCustomersByName():request:{customerName-{}}", customerName);
        try {
            List<MdsArCustomers> mdsArCustomerList = mdsArCustomersDao.getErpMdsArCustomersByName(customerName);
            if (mdsArCustomerList == null || mdsArCustomerList.size() < 1) {
                return noData("getErpArCustomersByName");
            }
            return wrapmdsArCustomerData2XML(mdsArCustomerList);
        } catch (Exception e) {
            log.error("getErpArCustomersByName():errorCause-{},errorMessage-{}", e.getCause(), e.getMessage());
            return errorMsg("getErpArCustomersByName error-{}", e.getMessage());
        }
    }

    /**
     * 按客户名称模糊查询获取ERP客户数量
     * @param customerName  客户名称
     * @return              客户数量
     */
    @Override
    public int getErpMdsArCustomersCount(String customerName) {
        int customersCount = 0;
        log.info("getErpMdsArCustomersCount():request:{customerName-{}}", customerName);
        try {
            customersCount = mdsArCustomersDao.getErpMdsArCustomersCount(customerName);
        } catch (Exception e) {
            log.error("getErpMdsArCustomersCount():errorCause-{},errorMessage-{}", e.getCause(), e.getMessage());
        }
        return customersCount;
    }

    private String wrapmdsArCustomerData2XML(List<MdsArCustomers> mdsArCustomerList) {
        Document root = null;
        root = DocumentHelper.createDocument();
        Element element = root.addElement("service");
        element.addElement("name").setText(" 客户基本资料");
        element.addElement("type").setText("发布服务");
        Element datalist = element.addElement("datalist");
        for (int i = 0; i < mdsArCustomerList.size(); i++) {
            MdsArCustomers mdsArCustomer = mdsArCustomerList.get(i);
            Element data = datalist.addElement("data");
            data.addElement("customerId").setText(StringUtil.convertToString(mdsArCustomer.getCustomerId()));
            data.addElement("customerNumber").setText(StringUtil.convertToString(mdsArCustomer.getCustomerNumber()));
            data.addElement("customerName").setText(StringUtil.convertToString(mdsArCustomer.getCustomerName()));
            data.addElement("accountName").setText(StringUtil.convertToString(mdsArCustomer.getAccountName()));
            data.addElement("status").setText(StringUtil.convertToString(mdsArCustomer.getStatus()));
            data.addElement("partyType").setText(StringUtil.convertToString(mdsArCustomer.getPartyType()));

            data.addElement("country").setText(StringUtil.convertToString(mdsArCustomer.getCountry()));
            data.addElement("address1").setText(StringUtil.convertToString(mdsArCustomer.getAddress1()));
            data.addElement("address2").setText(StringUtil.convertToString(mdsArCustomer.getAddress2()));
            data.addElement("address3").setText(StringUtil.convertToString(mdsArCustomer.getAddress3()));
            data.addElement("city").setText(StringUtil.convertToString(mdsArCustomer.getCity()));
            data.addElement("postalCode").setText(StringUtil.convertToString(mdsArCustomer.getPostalCode()));
            data.addElement("state").setText(StringUtil.convertToString(mdsArCustomer.getState()));
            data.addElement("province").setText(StringUtil.convertToString(mdsArCustomer.getProvince()));

            data.addElement("creationDate").setText(StringUtil.date2String(mdsArCustomer.getCreationDate()));
            data.addElement("createdBy").setText(StringUtil.convertToString(mdsArCustomer.getCreatedBy()));
            data.addElement("lastUpdateDate").setText(StringUtil.date2String(mdsArCustomer.getLastUpdateDate()));
            data.addElement("lastUpdatedBy").setText(StringUtil.convertToString(mdsArCustomer.getLastUpdatedBy()));

            data.addElement("requestId").setText(StringUtil.convertToString(mdsArCustomer.getRequestId()));
            data.addElement("reuqestedDate").setText(StringUtil.date2String(mdsArCustomer.getReuqestedDate()));
            data.addElement("internalFlag").setText(StringUtil.convertToString(mdsArCustomer.getInternalFlag()));

            data.addElement("attribute1").setText(StringUtil.convertToString(mdsArCustomer.getAttribute1()));
            data.addElement("attribute2").setText(StringUtil.convertToString(mdsArCustomer.getAttribute2()));
            data.addElement("attribute3").setText(StringUtil.convertToString(mdsArCustomer.getAttribute3()));
            data.addElement("attribute4").setText(StringUtil.convertToString(mdsArCustomer.getAttribute4()));
            data.addElement("attribute5").setText(StringUtil.convertToString(mdsArCustomer.getAttribute5()));
            data.addElement("attribute6").setText(StringUtil.convertToString(mdsArCustomer.getAttribute6()));
            data.addElement("attribute7").setText(StringUtil.convertToString(mdsArCustomer.getAttribute7()));
            data.addElement("attribute8").setText(StringUtil.convertToString(mdsArCustomer.getAttribute8()));
            data.addElement("attribute9").setText(StringUtil.convertToString(mdsArCustomer.getAttribute9()));
            data.addElement("attribute10").setText(StringUtil.convertToString(mdsArCustomer.getAttribute10()));
        }
        return root.asXML();
    }

    /**
     * 返回空数据的时候的信息
     *
     * @return
     */

    private String noData(String serviceName) {
        return "<?xml version=\"1.0\" encoding=\"UTF-8\"?><service><name>" + serviceName + "</name><type>发布服务</type><datalist><!--查无数据--></datalist></service>";
    }

    /**
     * 返回查询出错信息
     *
     * @param msg
     * @return
     */
    private String errorMsg(String serviceName, String msg) {
        return "<?xml version=\"1.0\" encoding=\"UTF-8\"?><service><name>" + serviceName + "</name><type>发布服务</type><datalist><!--error Message:" + msg + "--></datalist></service>";
    }
}
